//
//  WCG_Mine_ViewController.m
//  Project_B
//
//  Created by 黑黑 on 15/11/24.
//  Copyright © 2015年 Atom_Wang. All rights reserved.
//

#import "WCG_Mine_ViewController.h"
#import "Masonry.h"
#import "SettingViewController.h"
#import "MySpeViewController.h"
#import "GXProviderDetailTableViewCell.h"
#import "WCG_O_Detail_DetailProviderViewController.h"
#import "MyLikeViewController.h"

@interface WCG_Mine_ViewController () <UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate>

//  头部视图
@property (nonatomic, strong) UIView *headerView;
//  头像
@property (nonatomic, strong) UIImageView *imageView;

//  对话按钮
@property (nonatomic, strong) UIButton *messageBtn;
//  设置按钮
@property (nonatomic, strong) UIButton *settingBtn;

@property (nonatomic, strong) SettingViewController *settingVC;

@property (nonatomic, strong) UIView *blackView;

@property (nonatomic, strong) NSMutableArray *dataArr;
@end

@implementation WCG_Mine_ViewController

- (void)createBlackView {
    self.blackView = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.blackView setBackgroundColor:[UIColor blackColor]];
    self.blackView.alpha = 0.9;
    self.blackView.hidden = YES;
    [self.view addSubview:self.blackView];
}

- (SettingViewController *)settingVC {
    if (!_settingVC) {
        self.settingVC = [[SettingViewController alloc] init];
        self.settingVC.view.layer.cornerRadius = 5;
        self.settingVC.view.layer.masksToBounds = YES;
    }
    return _settingVC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(blackViewHidden) name:@"CloseAction" object:nil];
    
    self.navigationController.navigationBarHidden = YES;

    [self createTableView];
    [self createBlackView];
    // 调整bug通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeSetting) name:@"closeAction" object:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    UIView *temp = self.tabBarController.view.subviews[2];
    [UIView animateWithDuration:1 animations:^{
        self.navigationController.navigationBarHidden = YES;
        temp.frame = CGRectMake(0, kScreenHeight - 49, kScreenWidth, 49);
    }];
    
}

- (void)blackViewHidden {
    [UIView animateWithDuration:1.5f animations:^{
        self.blackView.alpha = 0;
    }completion:^(BOOL finished) {
        self.blackView.hidden = YES;
        
    }];
}

- (void)getData
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"SpecialModel" inManagedObjectContext:self.manager.managedObjectContext];
    [fetchRequest setEntity:entity];
#if 1
    // Specify criteria for filtering which objects to fetch
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"type = %@", @"prolike"];
    [fetchRequest setPredicate:predicate];
    // Specify how the fetched objects should be sorted
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
#endif
    NSError *error = nil;
    NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        NSLog(@"空");
    }
    for (SpecialModel *spe in fetchedObjects) {
        [self.dataArr addObject:spe];
    }
    [self.tableView reloadData];
}

- (void)createTableView {
    [self createTableViewWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64)];
    self.tableView.delegate = self;
    self.tableView.bounces = NO;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerClass:[GXProviderDetailTableViewCell class] forCellReuseIdentifier:@"procell"];
    [self createHeaderView];
}

#pragma mark - 头部视图
- (void)createHeaderView {
    //  创建头部视图
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
    //  设置为 tabelView 的头部视图
    self.tableView.tableHeaderView = self.headerView;
    
    //  头像
     self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
    self.imageView.center = self.headerView.center;
    //  头像点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    tap.delegate = self;

    [self.imageView setUserInteractionEnabled:YES];
    [self.imageView addGestureRecognizer:tap];
    [self.imageView setImage:[UIImage imageNamed:@"special-has-contents-updated"]];
    self.imageView.layer.cornerRadius = 40;
    self.imageView.layer.masksToBounds = YES;
    [self.headerView addSubview:self.imageView];
    
    //  对话按钮
    self.messageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.messageBtn setImage:[UIImage imageNamed:@"icon-message"]
                     forState:UIControlStateNormal];
    [self.headerView addSubview:self.messageBtn];
    [self.messageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.imageView.mas_left).offset(-60);
        make.size.mas_equalTo(CGSizeMake(30, 30));
        make.centerY.equalTo(self.imageView);
    }];
    
    //  设置按钮
    self.settingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.settingBtn setImage:[UIImage imageNamed:@"icon-setting"]
                     forState:UIControlStateNormal];
    [self.settingBtn addTarget:self
                        action:@selector(settingBtnAction)
              forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:self.settingBtn];
    [self.settingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imageView.mas_right).offset(60);
        make.size.equalTo(self.messageBtn);
        make.centerY.equalTo(self.messageBtn);
    }];
}

#pragma mark - 头像点击响应方法
- (void)tapAction {
    
}

#pragma mark - 设置按钮响应方法
- (void)settingBtnAction {
    
    
    [self.settingVC show];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [UIView animateWithDuration:1.5f animations:^{
            self.blackView.alpha = 1;
            
            self.blackView.hidden = NO;
        }];
    });
}

#pragma mark - tableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#if 1
    return 2;
#endif
#if 0
    return self.dataArr.count + 1;
#endif
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.textLabel.text = @"我订阅的专题";
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        return cell;
    } else if (indexPath.row == 1){
#if 1
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.textLabel.text = @"我喜欢的文章";
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        return cell;
#endif
#if 0
        GXProviderDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"procell"];
        [cell.detailImageView sd_setImageWithURL:[NSURL URLWithString:[self.dataArr[indexPath.row - 1] title_font]]];
        cell.titleLabel.text = [self.dataArr[indexPath.row - 1] title];
        cell.specialLabel.text = @"";
        cell.nameLabel.text = [self.dataArr[indexPath.row - 1] content_update_time];
        return cell;
#endif
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        MySpeViewController *speVC = [[MySpeViewController alloc] init];
        [self.navigationController pushViewController:speVC animated:YES];
    }else
    {
#if 0
        WCG_O_Detail_DetailProviderViewController *detailVC = [[WCG_O_Detail_DetailProviderViewController alloc] init];
        detailVC.link = [self.dataArr[indexPath.row - 1] share_text];
        detailVC.title = [self.dataArr[indexPath.row - 1] title];
        [self.navigationController pushViewController:detailVC animated:YES];
#endif
#if 1
        MyLikeViewController *likeVC = [[MyLikeViewController alloc] init];
        
       [self.navigationController pushViewController:likeVC animated:YES];
#endif
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
#if 1
    return 50;
#endif
#if 0
    if (indexPath.row == 0) {
        return 50;
    }else
    {
        return 120;
    }
#endif
}

@end
